'''
 * @ author     ：廖传港
 * @ date       ：Created in 2020/10/29 16:38
 * @ description：
 * @ modified By：
 * @ ersion     : 
 * @File        : test.py 
'''

import loading_pictures as lo
import numpy as np
import os
import struct
from keras.utils import to_categorical


# 加载数据集
def load_mnist(path, kind='train'):

    labels_path = os.path.join(path,
                               '%s-labels.idx1-ubyte'
                               % kind)
    images_path = os.path.join(path,
                               '%s-images.idx3-ubyte'
                               % kind)

    with open(labels_path, 'rb') as lbpath:
        magic, n = struct.unpack('>II',
                                 lbpath.read(8))
        labels = np.fromfile(lbpath,
                             dtype=np.uint8)

    with open(images_path, 'rb') as imgpath:
        magic, num, rows, cols = struct.unpack('>IIII',
                                               imgpath.read(16))
        images = np.fromfile(imgpath,
                             dtype=np.uint8).reshape(len(labels), 784)

    # print("images---->", images)
    # print("labels---->", labels)

    return images, labels


# --------------------------------------------------


# 加载数据集
def LoadMNIST():
    [train_images, train_labels] = load_mnist('D:/python/data/MNIST')

    # reshape：改变数组维数 重新塑造 矩阵变维
    XX = np.reshape(train_images, (60000, 28, 28))

    # 总数
    count = 200

    X = np.array(XX[0:count, :])

    X = X / 255.0

    Y = np.array(train_labels[0:count, ], int)

    Y = to_categorical(Y)

    return [X, Y]


X, Y = LoadMNIST()
print(X[5].shape)
# print("X--------->",X)
# print("Y--------->",Y)
X, Y = lo.loaddata("D:/python/data/")
print(X[6].shape)
print("X2--------->",X[4])
# print("Y2--------->",Y)

f = open('C:/Users/LCG/Desktop/data.txt', 'rw')

f.write('X[4]')

f.close()

# YY = np.zeros(Y.shape[0], )
#
# for i in range(Y.shape[0]):
#     idx = np.where(Y[i, :] > 0)
#
#     YY[i] = idx[0] / 10